# -*- coding:utf-8 -*-

from django.http import HttpResponse, HttpResponseRedirect
from django.views.generic.simple import direct_to_template
from django.shortcuts import render_to_response
from django.template import RequestContext
from tsuchinoco.schedule.models import Plan, PlanForm
def plan_index(request):
    """
    タスクの一覧を表示する
    """
    if request.method == 'GET':
        if request.session.get('user_id',None) == None:
            return HttpResponseRedirect('/')
        plans = Plan.objects.all()
        print Plan.objects.all().count()
        return render_to_response('calindex.html',
                                  context_instance=RequestContext(request,
                                                                  {'user_id':request.session.get('user_id',
                                                                                                 None),
                                                                   'plan_list':plans}))
    elif request.method == 'POST':
        pass

def newedit(request):
    """
    """
    user_id = request.session.get('user_id', None)
    if request.method == 'GET':
        form = PlanForm()
        print form
        return render_to_response('caledit.html',
                                  context_instance=RequestContext(request,
                                                                  {'form':form,
                                                                   'user_id':user_id,
                                                                   'error_message':''}))
    elif request.method == 'POST':
        plan = Plan()
        plan.owner = user_id
        form = PlanForm(request.POST,instance=plan)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/plan/')
        else:
            print form.errors
            return render_to_response('caledit.html',
                                      context_instance=RequestContext(request,
                                                                      {'form':form,
                                                                       'user_id':user_id,
                                                                       'error_message':form.errors}))
def plan_edit(request,user_id,plan_id):
    """
    """
    if request.method == 'GET':
        print user_id
        print request.session.get('user_id',None)
        if int(user_id) == request.session.get('user_id',0):
        #print  "task_id:%d"%task_id
            plans = Plan.objects.get(pk=int(plan_id))
            print plans.detail
            form = PlanForm(None,instance=plans)
            return render_to_response('calview.html',
                                      context_instance=RequestContext(request,
                                                                      {'form':form,
                                                                       'plan_id':plan_id,
                                                                       'user_id':user_id,
                                                                       'error_message':''}))
        else:
           pass
    elif request.method == 'POST':
        plans = Plan.objects.get(pk=int(plan_id))
        form = PlanForm(request.POST, instance=plans)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/plan/')
        else:
            print form.errors
            return render_to_response('calview.html',
                                      context_instance=RequestContext(request,
                                                                      {'form':form,
                                                                       'plan_id':plan_id,
                                                                       'user_id':user_id,
                                                                       'error_message':form.errors}))
 
